package com.guigu.lc.controller.sys;

import com.guigu.common.pojo.SysUsers;
import com.guigu.common.vo.SearchVo;
import com.guigu.lc.serach.UserSerach;
import com.guigu.lc.service.UserService;
import com.guigu.lc.view.usergentroleview;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ByteSource;
import org.springframework.http.client.MultipartBodyBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Controller
@RequestMapping("/sys")
@ResponseBody
public class UserController {

    @Resource
    private UserService userService;

    /**
     *查询所有的用户
     * @param searchVo
     * @return
     */
    @RequestMapping("/queryallusershow")
    public UserSerach queryallusershow(UserSerach searchVo){
        return this.userService.queryalluserService(searchVo);
    }

    //查询当前用户所拥有的角色，查询授予用户拥有的角色
    @RequestMapping("/usergentroleselect")
    public usergentroleview usergentroleselect(int id){
        Subject subject = SecurityUtils.getSubject();
        SysUsers user1 = (SysUsers) subject.getSession().getAttribute("user");
        return this.userService.selectusergentroleService(user1.getId(),id);
    }
    //授权收回账号角色
    @RequestMapping("/grantwithdrawuserrole")
    public int grantwithdrawuserrole(@RequestBody() usergentroleview usergentroleview){
        System.out.println(usergentroleview);
        this.userService.grantwithdrawuserroleService(usergentroleview);
        return 1;
    }

    //修改头像
    @RequestMapping("/xiugaitouxiang")
    public SysUsers xiugaitouxiang(MultipartFile imgfile, HttpSession session)  {
        try {


        String realPath = session.getServletContext().getRealPath("img");

        File savefile=new File(realPath);
        if(!savefile.exists()){
            savefile.mkdir();
        }
        String oldname=imgfile.getOriginalFilename();

        String newname= UUID.randomUUID()+oldname.substring(oldname.lastIndexOf("."));
        String savaPath= realPath+"\\"+newname;
        //新图片文件
        File newfile = new File(savaPath);
        //把上传存在内存中的文件内容写入目标文件中
        imgfile.transferTo(newfile);

        Subject subject = SecurityUtils.getSubject();
        SysUsers user1 = (SysUsers) subject.getSession().getAttribute("user");
        user1.setPhotoPath("/img/"+newname);
        this.userService.updateloginByidService(user1);
        session.setAttribute("user",user1);
        return user1;
        }catch (Exception e){
            e.printStackTrace();
//            return "/img/"+newname;
        }
        return null;
    }

    //查询输入密码是否与数据库密码是否一致
    @RequestMapping("/checkpasswordmm")
    public int checkpasswordmm(String password){
        Subject subject = SecurityUtils.getSubject();
        SysUsers user1 = (SysUsers) subject.getSession().getAttribute("user");
        Object ob1=new SimpleHash("MD5",password,ByteSource.Util.bytes(user1.getLoginId()),1024);
        if(ob1.toString().equals(user1.getPassword()) ||ob1.toString()==user1.getPassword()){
            return 1;
        }else{
            return 2;
        }
    }
    //修改密码
    @RequestMapping("/updatepasswordsys")
    public int updatepasswordsys(String password){
        Subject subject = SecurityUtils.getSubject();
        SysUsers user1 = (SysUsers) subject.getSession().getAttribute("user");
        Object ob1=new SimpleHash("MD5",password,ByteSource.Util.bytes(user1.getLoginId()),1024);
        SysUsers s=new SysUsers();
        s.setId(user1.getId());
        s.setPassword(ob1.toString());
        this.userService.updatepasswordsys(s);
        return 1;
    }

    //注册账号
    @RequestMapping("/insertuserzc")
    public int insertuserzc(SysUsers users){
        Object ob1=new SimpleHash("MD5",users.getPassword(),ByteSource.Util.bytes(users.getLoginId()),1024);
        SysUsers s=new SysUsers();
        s.setLoginId(users.getLoginId());
        s.setPassword(ob1.toString());
        this.userService.insertuserzcService(s);
        return 1;
    }
}
